package com.szxy.miaosha.controller;

import com.szxy.miaosha.domain.MiaoshaUser;
import com.szxy.miaosha.domain.Users;
import com.szxy.miaosha.service.UsersService;
import com.szxy.miaosha.utils.CodeMsg;
import com.szxy.miaosha.utils.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auther:wzer
 * @Date:2020/9/5
 * @Description:com.szxy.miaosha.controller
 * @Version:1.0
 **/
@RestController
@RequestMapping("/user")
public class UsersController {

    @Autowired
    private UsersService usersService;

    private Logger logger = LoggerFactory.getLogger(UsersController.class);

    @GetMapping("/getUserInfo")
    public Result<MiaoshaUser> getUserInfo(MiaoshaUser miaoshaUser) {
        logger.info(miaoshaUser.toString());
        return Result.success(miaoshaUser);
    }

    @RequestMapping("/findUserById/{id}")
    public Result<Users> findUserById(@PathVariable int id) {
        Users user = this.usersService.selectUserById(id);
        return Result.success(user);
    }

    @RequestMapping("/addUser/{id}/{name}")
    public Result addUser(@PathVariable int id, @PathVariable String name) {
        boolean flag = this.usersService.insertUser(new Users(id, name));
        return flag ? Result.ok() : Result.error(CodeMsg.ADD_USER_ERROR);
    }

    @RequestMapping("/addManyUser")
    public Result addUser() {
        boolean flag = this.usersService.insertUser();
        return flag ? Result.ok() : Result.error(CodeMsg.ADD_USER_ERROR);
    }


}
